import Common from '../common'
var Js = Common('paper')
export default {
	name: 'paper',
	components: {},
	mixins: [Js],
	data() {
		return {
			mainData: [],
			self: this,

			table_arguments: {
				height: '70%',
				row_key: 'id',
				tree_props: {
					children: 'child',
					child: 'child',
					hasChildren: 'child'
				},
				default_expand_all: false,
				expand: false,
				selection: true,
				cell_style: {},
				loading: true
			},
			fields: [{
					key: 'id',
					label: '试卷ID',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width: 80
				},
				{
					key: 'title',
					label: '试卷名称',
					application: ['编辑', '生成试卷'],
					componentName: 'sls-input',
					listType: 'normal',
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入试卷名称',
						clearable: true,
						defaultValue: '',
						optionsName: '',
						changeFunc: function(val, self) {
							if (val) {
								self.searchItem.title = ['LIKE', ['%' + val + '%']];
							} else {
								delete self.searchItem.title;
							};
						},
					},
				},
				{
					key: 'all_time',
					label: '考试时长（分钟）',
					application: ['编辑', '生成试卷'],
					componentName: 'sls-input',
					listType: 'normal',
				},
				{
					key: 'all_score',
					label: '试卷总分',
					application: ['编辑', '生成试卷'],
					componentName: 'sls-input',
					listType: 'normal',
					width: 200
				},
				// {
				//   key: 'is_template',
				//   label: '是否模板',
				//   application: ['编辑'],
				//   componentName: 'sls-input',
				//   listType: '',
				// },
				{
					key: 'listorder',
					label: '排序',
					application: ['编辑', '生成试卷'],
					componentName: 'sls-input',
					listType: 'normal',
				},
				// {
				// 	key: 'mainImg',
				// 	label: '图片',
				// 	application: ['编辑', '生成试卷'],
				// 	componentName: 'qi-upload',
				// 	listType: '',
				// 	limit: 10,
				// 	dialogStyle: 'width:100%;',
				// },
				// {
				// 	key: 'type1',
				// 	label: '单选题目数',
				// 	application: ['生成试卷'],
				// 	componentName: 'sls-input',
				// },
				// {
				// 	key: 'score1',
				// 	label: '单选题分数',
				// 	application: ['生成试卷'],
				// 	componentName: 'sls-input',
				// },
				// {
				//   key: 'type2',
				//   label: '多选题目数',
				//   application:['生成试卷'],
				//   componentName:'sls-input',
				// },
				// {
				//   key: 'score2',
				//   label: '多选题分数',
				//   application:['生成试卷'],
				//   componentName:'sls-input',
				// },
				// {
				//   key: 'type3',
				//   label: '填空题目数',
				//   application:['生成试卷'],
				//   componentName:'sls-input',
				// },
				// {
				//   key: 'score3',
				//   label: '填空题分数',
				//   application:['生成试卷'],
				//   componentName:'sls-input',
				// },
				// {
				// 	key: 'type4',
				// 	label: '判断题目数',
				// 	application: ['生成试卷'],
				// 	componentName: 'sls-input',
				// },
				// {
				// 	key: 'score4',
				// 	label: '判断题分数',
				// 	application: ['生成试卷'],
				// 	componentName: 'sls-input',
				// },
				// {
				// 	key: 'type5',
				// 	label: '解答题目数',
				// 	application: ['生成试卷'],
				// 	componentName: 'sls-input',
				// },
				// {
				// 	key: 'score5',
				// 	label: '解答题分数',
				// 	application: ['生成试卷'],
				// 	componentName: 'sls-input',
				// },
				{
					key: "status",
					label: '状态',
					application: [],
					componentName: 'sls-select',
					optionsName: 'statusOptions',
					defaultProps: {
						label: 'text',
						value: 'value',
					},
					formatter: function(val) {
						if (val.status == 1) {
							return '启用';
						} else {
							return '关闭';
						};
					}
				},
				{
					key: 'create_time',
					label: '创建时间',
					listType: 'normal',
					placeholder: '请选择创建时间',
					header_search: {
						componentName: 'sls-date-time-range',
						style: 'width:160px;margin-right:2px;',
						start_placeholder: '创建开始时间',
						end_placeholder: '创建结束时间',
						defaultValue: [],
						changeFunc: function(value, self) {
							if (!value) {
								delete self.searchItem.create_time;
							} else {
								self.searchItem.create_time = ['between', value = value.map(function(e) {
									return e / 1000;
								})];
							};
						},
					},
					width: 200
				},
				{
					label: '操作',
					listType: 'deal',
					width: 200
				},
			],
			// 按钮配置
			btn_info: [


				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '编辑'
					},
					func: {
						apiName: function(self) {
							return "api_testPaperUpdate"
						},
						formData: function(self) {
							return self.formData

						},
						postData: function(self) {

							var postData = {
								searchItem: {
									id: self.formData.id,
									user_no: self.formData.user_no
								},
								data: self.submitData
							};
							if (self.submitData.parentid && self.submitData.parentid == self.formData.id) {
								self.$$notify('父级ID和子级ID重叠', 'fail');
								return false;
							} else {
								return postData;
							};

						}
					},
				},

				{
					type: 'danger',
					icon: 'delete',
					size: 'mini',
					funcType: 'submit',
					position: 'header',
					text: function(data) {
						return '删除选中'
					},
					func: {
						apiName: function(self) {
							return "api_testPaperUpdate"
						},
						postData: function(self) {
							var deleteArray = [];
							for (var i = 0; i < self.selectionArray.length; i++) {
								deleteArray.push(self.selectionArray[i].id);
							};
							var postData = {
								searchItem: {
									id: ['in', deleteArray]
								},
								data: {
									status: -1
								}
							};
							return postData;
						}

					},
				},

				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'header',
					text: function(data) {
						return '生成试卷'
					},
					func: {
						apiName: function(self) {
							return "api_testPaperAdd"
						},
						formData: function(self) {
							var data = {
								title: '',
								description: '',
								mainImg: [],
							};
							return data
						},
						postData: function(self) {
							self.submitData.relation_id = self.id;
							self.submitData.type = 1;
							self.submitData.course = self.course;
							var postData = {
								data: self.submitData
							};
							return postData;
						}
					},
				},
				// {
				// 	type: 'info',
				// 	icon: 'edit',
				// 	size: 'mini',
				// 	position: 'list',
				// 	text: function(data) {
				// 		return '参加考试'
				// 	},
				// 	styleFun: function(data) {
				// 		return 'background_color:#209cd1;border-color:#209cd1'
				// 	},
				// 	funcType: 'func',
				// 	func: {
				// 		func: function(self) {
				// 			self.$router.push({
				// 				path: '/home/exam/' + self.originFormData.id,
				// 				name: '考试管理',
				// 				params: {
				// 					id: self.originFormData.id,
				// 					path: '/home/paper',
				// 					news: true,
				// 					delKeepAlive: true
				// 				}
				// 			});
				// 		},
				// 	},
				// },
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '管理题目(试卷)'
					},
					styleFunc: function(data) {
						if (!data.children) {
							return 'background-color: #3CB371;border-color:#3CB371'
						} else {
							return 'display:none'
						}
					},
					funcType: 'func',
					func: {
						func: function(self) {

              console.log('self.originFormData.id',self.originFormData.id)
							self.$router.push({
								path: '/home/paperSubject/',
								name: '管理题目(试卷)',
								params: {
									id: self.originFormData.id,
									path: '/home/paperSubject',
									backPath: '/home/paper',
									news: true,
									delKeepAlive: true
								}
							});
						},
					},
				},

				// {
				// 	type: 'info',
				// 	icon: 'edit',
				// 	size: 'mini',
				// 	position: 'header',
				// 	noAuth: true, //此字段含义为不需要权限控制，所有人默认都可看见
				// 	text: function(data) {
				// 		return '返回'
				// 	},
				// 	funcType: 'func',
				// 	func: {
				// 		func: function(self) {
				// 			var fullPath = self.$route.fullPath;
				// 			var tabList = self.$store.state.tabs.tabs;
				// 			var routerRes = self.$$getBackRoute(tabList, fullPath);
				// 			self.$store.commit('REMOVE_TABS', {
				// 				fullPath: fullPath,
				// 			});
				// 			if (routerRes.backRes) {
				// 				self.$router.push(routerRes.backRes);
				// 			} else {
				// 				self.$router.push(routerRes.newRes);
				// 			};
				// 		},
				// 	},
				// },

			],
			paginate: {
				count: 0,
				currentPage: 1,
				pagesize: 10,
				is_page: true,
				page_sizes: [10, 30, 60, 90],
				layout: 'total, sizes, prev, pager, next, jumper',
			},
			searchItem: {
				thirdapp_id: this.$store.getters.getUserinfo.thirdapp_id,
				parentid: 0
				// type: 1
			},
			optionData: {
				labelOptions: [],
				typeOptions: [{
						text: '随堂检测',
						value: 0
					}, {
						text: '章节测试',
						value: 1
					},
					{
						text: '结课考核',
						value: 2
					}
				],
				statusOptions: [{
					text: '启用',
					value: 1
				}, {
					text: '禁用',
					value: -1
				}],
			},
			otherData: {},
			UserInfo: {
				tableName: 'UserInfo',
				searchItem: {},
				fixSearchItem: {
					status: 1
				},
				key: 'user_no',
				middleKey: 'user_no',
				condition: 'in',
			},
			dialog: {
				formLabelWidth: 'auto',
				dialogFormVisible: false,
				title: '用户'
			},
			getBefore: {},
			formLabelWidth: 'auto',
			btnName: '',
			formData: {},
			btnNow: {},
			submitData: {},
			orginFormData: {},

			selectionArray: []

		}

	},

	mounted() {
		this.init()
	},
	computed: {
		token: function() {
			return this.$store.getters.getToken
		}
	},
	watch: {
		$route(to, from) {},
		token() {

		}
	},
	methods: {

		/**
		 * 初始化
		 */
		init() {

			this.id = this.$route.params.id;
			this.course = this.$route.params.course;
			// this.type = this.$route.params.type;
			// this.path = this.$route.params.path;
			this.searchItem.relation_id = this.id;

			this.initMainData();

		},

		clearSearch() {
			const self = this;
			self.searchItem = {
				relation_id: self.id,
				thirdapp_id: this.$store.getters.getUserinfo.thirdapp_id
			};
			self.initMainData(true);
		},


		/**
		 * 列表主函数
		 */



		async initMainData(isNew) {

			const self = this;
			self.table_arguments.loading = true;
			const postData = {};
			if (isNew) {
				self.paginate.currentPage = 1;
			};
			postData.paginate = self.$$cloneForm(self.paginate);
			postData.token = self.$store.getters.getToken;
			if (self.searchItem) {
				postData.searchItem = self.$$cloneForm(self.searchItem)
			};
			if (JSON.stringify(self.getBefore) != "{}") {
				postData.getBefore = self.$$cloneForm(self.getBefore);
			};
			postData.order = {
				listorder: 'asc'
			}
			var res = await self.$$api_testPaperGet({
				data: postData
			});
			// res.info.data.forEach((item,index)=>{
			// 	item.type = item.all_time/60;
			// })
			self.mainData = res.info.data;

			self.paginate.count = res.info.total;
			self.table_arguments.loading = false;
		},


	},


}
